import Cookies from "js-cookie";
import { message } from "ant-design-vue";

const TOKEN = localStorage.getItem("token");

/**
 * 设置 cookie
 * @param {*} name
 * @param {*} value
 * @param {*} expires 默认24小时有效期
 * @param {*} path
 */
export const setCookie = (name, value, expires = 1, path = "/") => {
  Cookies.set(name, value, {
    expires,
    path,
  });
};

/**
 * 获取 cookie
 * @param {*} name
 * @returns
 */
export const getCookie = (name) => {
  return Cookies.get(name) || "";
};

/**
 * 清楚 cookie
 * @param {*} name
 * @param {*} path
 */
export function removeCookie(name, path = "/") {
  Cookies.remove(name, {
    path,
  });
}

/**
 * 跳转登录界面，登录失效
 */
export const goLogin = () => {
  message.warning({
    content: "登录失效，请重新登录！",
    duration: 2,
    onClose: () => {
      // removeCookie(TOKEN);
      const location = window.location;
      let href = location.origin;
      window.location.replace(`${href}#/login`);
    },
  });
};

export const _extends =
  Object.assign ||
  function (target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];
      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }
    return target;
  };
